; 此程序是写来测试lib.inc的几个函数的。
; 代码有误导致程序运行不出来，用这个程序来单步调试lib里的程序哪里出了错。
%include "pm.inc"

%ifdef DOS_DEBUG
org 0100h
%else
org 07c00h
%endif

%define TEST_DISP_STR


PageDirBase equ 200000h
PageTblBase equ 201000h

jmp LABEL_BEGIN

[SECTION .gdt]
LABEL_GDT: Descriptor 0, 0, 0
LABEL_DESC_CODE_TEST: Descriptor 0, CodeLen - 1, DA_C + DA_32
LABEL_DESC_DATA: Descriptor 0, DataLen - 1, DA_DRW
LABEL_DESC_STACK: Descriptor 0, TopOfStack, DA_DRWA + DA_32

GdtLen equ $ - LABEL_GDT
GdtPtr dw GdtLen - 1
    dd 0

SelectorCodeTest equ LABEL_DESC_CODE_TEST - LABEL_GDT
SelectorData equ LABEL_DESC_DATA - LABEL_GDT
SelectorStack equ LABEL_DESC_STACK - LABEL_GDT

[SECTION .ss]
LABEL_STACK:
	times 32 db 0
TopOfStack equ $ - LABEL_STACK - 1

[SECTION .data1]
ALIGN 32
[BITS 32]
LABEL_DATA:
	_dwMCRNumber dd 6
	_dwMemSize dd 0
	_ARDStruct:
		_dwBaseAddrLow: dd 0
		_dwBaseAddrHigh: dd 0
		_dwLengthLow: dd 0
		_dwLengthHigh: dd 0
		_dwType : dd 0	
	_MemChkBuf: db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
			0x00, 0xf0, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, \
			0x01, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x09, 0x00, \
			0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, \
			0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, \
			0x00, 0x80, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, \
			0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, \
			0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, \
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef, 0x03, \
			0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, \
			0x00, 0x00, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, \
			0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, \
			0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, \
			0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, \
			0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00
	
	dwMemSize		equ	_dwMemSize	- $$
	dwMCRNumber		equ	_dwMCRNumber	- $$
	ARDStruct		equ	_ARDStruct	- $$
		dwBaseAddrLow	equ	_dwBaseAddrLow	- $$
		dwBaseAddrHigh	equ	_dwBaseAddrHigh	- $$
		dwLengthLow	equ	_dwLengthLow	- $$
		dwLengthHigh	equ	_dwLengthHigh	- $$
		dwType		equ	_dwType		- $$
	MemChkBuf		equ	_MemChkBuf	- $$
DataLen equ $ - LABEL_DATA
	
	


[SECTION .s16]
[BITS 16]
LABEL_BEGIN:
	mov ax, cs
	mov ds, ax
	mov es, ax
	mov ss, ax
	mov sp, 0100h
	
	mov eax, 0
	mov ax, cs
	shl eax, 4
	add eax, LABEL_CODE32
	mov [LABEL_DESC_CODE_TEST + 2], ax
	shr eax, 16
	mov [LABEL_DESC_CODE_TEST + 4], al
	mov [LABEL_DESC_CODE_TEST + 7], ah

	
	mov eax, 0
	mov ax, ds
	shl eax, 4
	add eax, LABEL_DATA
	mov [LABEL_DESC_DATA + 2], ax
	shr eax, 16
	mov [LABEL_DESC_DATA + 4], al
	mov [LABEL_DESC_DATA + 7], ah

	
	mov eax, 0
	mov ax, ds
	shl eax, 4
	add eax, LABEL_STACK
	mov [LABEL_DESC_STACK + 2], ax
	shr eax, 16
	mov [LABEL_DESC_STACK + 4], al
	mov [LABEL_DESC_STACK + 7], ah

	mov eax, 0
	mov ax, ds
	shl eax, 4
	add eax, LABEL_GDT
	mov dword [GdtPtr + 2], eax

	lgdt [GdtPtr]
	
	cli

	in al, 92h
	or al, 00000010b
	out 92h, al
	
	mov eax, cr0
	or al, 1
	mov cr0, eax

	jmp dword SelectorCodeTest:0


[SECTION .s32]
[BITS 32]
LABEL_CODE32:	
	mov ax, SelectorData
	mov ds, ax
	mov ax, SelectorData
	mov es, ax
	
	mov ax, SelectorStack
	mov ss, ax
	mov esp, TopOfStack
	
	call DispMemSize
	jmp $


DispMemSize:
	
	mov esi, MemChkBuf
	mov ecx, [dwMCRNumber]
.loop:
	mov edx, 5
	mov edi, ARDStruct
.1:
	push dword [esi]
	; call DispInt
	pop eax
	
	stosd
	
	add esi, 4
	dec edx
	cmp edx, 0
	jnz .1

	cmp dword [dwType], 1
	jne .2
	mov eax, [dwBaseAddrLow]
	add eax, [dwLengthLow]
	cmp eax, [dwMemSize]
	jb .2
	mov [dwMemSize], eax
.2:
	loop .loop
	
	
	push dword [dwMemSize]
	add esp, 4
	
	ret
	

CodeLen equ $ - LABEL_CODE32
